package com.hanson.service;

import com.hanson.entity.UserShareBean;
import com.hanson.mapper.UserShareBeanMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserShareService {
    @Autowired
    private UserShareBeanMapper userShareBeanMapper;

    @Async
    public void batchInsert(String openId, String goodsId, List<String> shareImgsList) {
        if (CollectionUtils.isNotEmpty(shareImgsList)) {
            shareImgsList.stream().forEach(c -> {
                UserShareBean userShareBean = new UserShareBean();
                userShareBean.setOpenId(openId);
                userShareBean.setGoodsId(Integer.parseInt(goodsId));
                userShareBean.setShareImage(c);
                userShareBeanMapper.insert(userShareBean);
            });
        }
    }

    public List<String> getShareImgs(String openId, String goodsId) {
        List<UserShareBean> userShareBeanList = userShareBeanMapper.selectByOpenIdAndGoodsId(openId, Integer.parseInt(goodsId));
        List<String> returnList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(userShareBeanList)) {
            userShareBeanList.stream().forEach(c -> {
                returnList.add(c.getShareImage());
            });
        }
        return returnList;
    }
}
